package service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import model.Message;
import model.MessageRece;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import dao.MessageDao;
import dao.MessageReceDao;

@Service
public class MessageService {
	@Autowired
	private MessageDao messageDao;
	@Autowired
	private MessageReceDao messageReceDao;
	private Message message;
	private MessageRece messageRece;

	public void saveOrUpdate(Message message, String receids, String opreation,
			String deleteids, HashMap<String, Object> map) {
		if (opreation.equals("save")) { /* 保存修改 */
			message.setStatus(1);// 草稿状态
			message.setLasttime(new Date());
			if (message.getId() == null) {
				messageDao.save(message);
			} else {
				messageDao.update(message);
			}
			Integer messageid = message.getId();
			/* 关联表 */
			if (StringUtils.isNotBlank(receids)) {
				String[] _receids = receids.split(",");
				// 删除
				messageReceDao.deleteByMessage(messageid);
				for (String receid : _receids) {
					Integer _receid = Integer.parseInt(receid);
					MessageRece _messageRece = new MessageRece();
					_messageRece.setMessageid(messageid);
					_messageRece.setReceiverid(_receid);
					_messageRece.setStatus(1);// 未读状态
					// 新增
					messageReceDao.save(_messageRece);
				}
			}
		} else if (opreation.equals("sand")) { /* 发送 */
			message.setStatus(2);// 已发送状态
			message.setSendtime(new Date());
			message.setLasttime(new Date());
			if (message.getId() == null) {/* 新建发送 */
				messageDao.save(message);
			} else {/* 列表发送 */
				// message = messageDao.getById(message.getId());
				// String fileFileName=(String) map.get("fileFileName");
				// String filePath=(String) map.get("filePath");
				// message.setAttach(filePath);
				// message.setAttachname(fileFileName);
				messageDao.update(message);
			}
			Integer messageid = message.getId();
			/* 关联表 */
			if (StringUtils.isNotBlank(receids)) {
				String[] _receids = receids.split(",");
				// 删除
				messageReceDao.deleteByMessage(messageid);
				for (String receid : _receids) {
					Integer _receid = Integer.parseInt(receid);
					MessageRece _messageRece = new MessageRece();
					_messageRece.setMessageid(messageid);
					_messageRece.setReceiverid(_receid);
					_messageRece.setStatus(1);// 未读状态
					// 新增
					messageReceDao.save(_messageRece);
				}
			}
		}
		// else if (opreation.equals("delete")) { /* 删除（保留关联表） */
		// if (StringUtils.isNotBlank(deleteids)) {
		// String[] _ids = deleteids.split(",");
		// for (String id : _ids) {
		// Integer _id = Integer.parseInt(id);
		// message = messageDao.getById(_id);
		// message.setStatus(3);
		// messageDao.update(message);
		// }
		// }
		// }
	}

	/**
	 * 删除自己创建草稿或已发送信息
	 */
	public void delete(String ids) {
		String[] _ids = ids.split(",");
		HashMap<String, Object> map = new HashMap<String, Object>();
		// 删除
		for (String item : _ids) {
			map.put("id", item);
			map.put("status", 3);
			messageDao.changeStatus(map);
		}
	}

	public PageList<HashMap<String, Object>> queryPage(int page, int limit,
			HashMap<String, Object> map) {
		PageBounds pageBounds = new PageBounds(page, limit);
		return (PageList<HashMap<String, Object>>) messageDao.queryPage(
				pageBounds, map);
	}

	public PageList<HashMap<String, Object>> queryMyPage(int page, int limit,
			HashMap<String, Object> map) {
		PageBounds pageBounds = new PageBounds(page, limit);
		return (PageList<HashMap<String, Object>>) messageDao.queryMyPage(
				pageBounds, map);
	}

	/**
	 * 读取信息，改变状态
	 * 
	 * @param map
	 */
	public void read(HashMap<String, Object> map) {
		messageReceDao.changeStatus(map);
	}

	/**
	 * 删除自己接收到的信息
	 * 
	 * @param map
	 */
	public void deleteMy(String ids) {
		String[] _ids = ids.split(",");
		HashMap<String, Object> map = new HashMap<String, Object>();
		// 删除
		for (String item : _ids) {
			map.put("id", item);
			map.put("status", 3);
			messageReceDao.changeStatus(map);
		}
	}

	/**
	 * 获取详细信息
	 * 
	 * @param id
	 * @return
	 */
	public Message getById(Integer id) {
		return message = messageDao.getById(id);
	}

	public PageList<HashMap<String, Object>> queryReceiver(int page, int limit,
			HashMap<String, Object> map) {
		PageBounds pageBounds = new PageBounds(page, limit);
		List<HashMap<String, Object>> list = messageDao.queryReceiver(
				pageBounds, map);
		// List<User> listUser= list.get
		// 获得结果集条总数
		PageList<HashMap<String, Object>> pageList = (PageList<HashMap<String, Object>>) list;
		return pageList;
	};

	public Integer msgCount(Integer userid) {
		return messageReceDao.msgCount(userid);
	}

	public List<HashMap<String, Object>> queryReceivers(Integer mesid) {
		return messageReceDao.getByMessage(mesid);
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public MessageRece getMessageRece() {
		return messageRece;
	}

	public void setMessageRece(MessageRece messageRece) {
		this.messageRece = messageRece;
	}

}
